﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Xml;
using UFSControl.FTPClient;
using Microsoft.Win32;
using System.Collections;


namespace UFSControl
{
    public partial class channelList : Form
    {
        const string userRoot = "HKEY_CURRENT_USER";
        const string subkey = "kRemote";
        const string keyName = userRoot + "\\" + subkey;
        const string keySect = "Pfad";

        private string ftpUri = "";
        private string ftpUser = "root";
        private string passwd = "";
        private string ftpfilePath = "ftproot/System/Programmliste (Channel List)/";
        private string ftpfileName = "channel.xml";
                                 
        //Liste Radio, TV
        List<KService> tvList = new List<KService>();
        List<KService> radioList = new List<KService>();
        KChannelFile ChannelFile = new KChannelFile();

        public channelList()
        {
            InitializeComponent();

            //Adresse einlesen
            IniFile lIni = new IniFile();
            var lip = lIni.Read("IP", "Receiver");
            ftpUri = lip; // "ftp://" + lip;
            //passwort auslesen
            passwd = lIni.Read("PASSWD", "Receiver");
        }

        //Pfad in Registry schreiben
        private void SafePath(string AFilename)
        {
            string Pfad = Path.GetFileNameWithoutExtension(AFilename);
            if (Pfad != "")
            {
                Registry.SetValue(keyName, keySect, Pfad);
            }
        }

        //Pfad aus Registry lesen
        private string LoadPath()
        {
            string Path = (string)Registry.GetValue(keyName, keySect, "c:\\");
            if (Path == "") { return "c:\\"; } else { return Path; }
        }

        private void datenLadenClick(object sender, EventArgs e)
        {
            FTPConnection ftpCon = new FTPConnection();
            string localFileName = Path.GetTempFileName();
            ftpCon.Open(ftpUri, ftpUser, passwd, FTPMode.Passive);
            ftpCon.SetCurrentDirectory(ftpfilePath);
            ftpCon.GetFile(ftpfileName, localFileName, FTPFileTransferType.Binary);
            ftpCon.Close();
            LoadFromFile(localFileName);
            dateiSpeichernMenuItem.Enabled = true;
        }

        //Datei laden
        private void LoadFromFile(string FileName)
        {
            //XML Dokument erstellen
            XmlDocument doc = new XmlDocument();
            //aus Datei lesen
            StreamReader sReader = new StreamReader(FileName);
            string test = sReader.ReadToEnd();

            try
            {
                doc.LoadXml(test);
                //doc.Load(Application.StartupPath + "\\channel.xml");
                XmlElement root = doc.DocumentElement;
                XmlNodeList nodes = root.SelectNodes("/Servicelist/Service"); // /Servicelist/Satellitelist/Service

                foreach (XmlNode i in nodes)
                {
                    KService lService = new KService();
                    ChannelFile.AddService(lService);
                    if (i["ChannelNo"] != null) { lService.ChannelNo = Convert.ToInt16(i["ChannelNo"].InnerText); }
                    if (i["Name"] != null) { lService.Name = i["Name"].InnerText; }
                    if (i["Sid"] != null) { lService.Sid = i["Sid"].InnerText; }
                    if (i["Tsid"] != null) { lService.Tsid = i["Tsid"].InnerText; }
                    if (i["Onid"] != null) { lService.Onid = i["Onid"].InnerText; }
                    if (i["Vpid"] != null) { lService.Vpid = i["Vpid"].InnerText; }
                    if (i["Apid"] != null) { lService.Apid = i["Apid"].InnerText; }
                    if (i["PCRpid"] != null) { lService.PCRpid = i["PCRpid"].InnerText; }
                    if (i["Scrambled"] != null) { lService.Scrambled = Convert.ToBoolean(i["Scrambled"].InnerText); }
                    if (i["Skipped"] != null) { lService.Skipped = Convert.ToBoolean(i["Skipped"].InnerText); }
                    if (i["Locked"] != null) { lService.Locked = Convert.ToBoolean(i["Locked"].InnerText); }
                    if (i["ServiceType"] != null)
                    {
                        if (i["ServiceType"].InnerText == "TV")
                        {
                            lService.KServiceType = ServiceType.TV;
                        }
                        else
                        {
                            lService.KServiceType = ServiceType.Radio;
                        }
                    }
                    //Carrier suchen
                    XmlNodeList lCarrierNodes = i.SelectNodes("./Carrier");
                    foreach (XmlNode lCNode in lCarrierNodes)
                    {
                        if (lCNode["Longitude"] != null) { lService.Carrier.Longitude = Convert.ToInt16(lCNode["Longitude"].InnerText); }
                        if (lCNode["Band"] != null) { lService.Carrier.Band = lCNode["Band"].InnerText; }
                        if (lCNode["Frequency"] != null) { lService.Carrier.Frequency = Convert.ToInt16(lCNode["Frequency"].InnerText); }
                        if (lCNode["SymbolRate"] != null) { lService.Carrier.SymbolRate = Convert.ToInt16(lCNode["SymbolRate"].InnerText); }
                        if (lCNode["Pol"] != null) { if (lCNode["Pol"].InnerText == "H") { lService.Carrier.Pol = Polarisation.H; } else { lService.Carrier.Pol = Polarisation.V; };}
                        if (lCNode["FEC"] != null) { lService.Carrier.FEC = lCNode["FEC"].InnerText; }
                    }
                    if (lService.KServiceType == ServiceType.TV)
                    {
                        tvList.Add(lService);
                        ListViewItem lItem = new ListViewItem(lService.Data());
                        lItem.Tag = lService;
                        tvListView.Items.Add(lItem);
                    }
                    else
                    {
                        radioList.Add(lService);
                        ListViewItem lItem = new ListViewItem(lService.Data());
                        lItem.Tag = lService;
                        radioListView.Items.Add(lItem);
                    }
                }
                // Satellitenliste ermitteln
                nodes = root.SelectNodes("/Servicelist/Satellitelist/Satellite");
                foreach (XmlNode i in nodes)
                {
                    KSatellite lSat = new KSatellite();
                    ChannelFile.AddSatellite(lSat);
                    if (i["SatName"] != null) { lSat.SatName = i["SatName"].InnerText; }
                    if (i["Longitude"] != null) { lSat.Longitude = i["Longitude"].InnerText; }
                    if (i["Band"] != null) { lSat.Band = i["Band"].InnerText; }
                    //Transponder suchen
                    XmlNodeList lTransponder = i.SelectNodes("./Transponder");
                    foreach (XmlNode lTNode in lTransponder)
                    {
                        string Frequency = "";
                        string SymbolRate = "";
                        string Onid = "";
                        string Tsid = "";
                        string Nid = "";
                        Polarisation Pol = Polarisation.H;
                        string FEC = "";
                        if (lTNode["Frequency"] != null) { Frequency = lTNode["Frequency"].InnerText; }
                        if (lTNode["SymbolRate"] != null) { SymbolRate = lTNode["SymbolRate"].InnerText; }
                        if (lTNode["Onid"] != null) { Onid = lTNode["Onid"].InnerText; }
                        if (lTNode["Tsid"] != null) { Tsid = lTNode["Tsid"].InnerText; }
                        if (lTNode["Nid"] != null) { Nid = lTNode["Nid"].InnerText; }
                        if (lTNode["Pol"] != null) { if (lTNode["Pol"].InnerText == "H") { Pol = Polarisation.H; } else { Pol = Polarisation.V; };}
                        if (lTNode["FEC"] != null) { FEC = lTNode["FEC"].InnerText; }
                        lSat.AddTransponder(Frequency, SymbolRate, Onid, Tsid, Nid, Pol, FEC);
                    }
                }
                // Konfigurationen ermitteln
                nodes = root.SelectNodes("/Servicelist/Satellitelist/Configuration");
                foreach (XmlNode i in nodes)
                {
                    KConfiguration lConfig = new KConfiguration();
                    ChannelFile.AddConfiguration(lConfig);
                    if (i["Slot"] != null) { lConfig.Slot = i["Slot"].InnerText; }
                    if (i["Tuner"] != null) { lConfig.Tuner = i["Tuner"].InnerText; }
                    if (i["Longitude"] != null) { lConfig.Longitude = i["Longitude"].InnerText; }
                    if (i["Band"] != null) { lConfig.Band = i["Band"].InnerText; }
                    if (i["LO1Frequency"] != null) { lConfig.LO1Frequency = i["LO1Frequency"].InnerText; }
                    if (i["LO2Frequency"] != null) { lConfig.LO2Frequency = i["LO2Frequency"].InnerText; }
                    if (i["BandSwitchFreq"] != null) { lConfig.BandSwitchFreq = i["BandSwitchFreq"].InnerText; }
                    if (i["DiSEqC"] != null) { lConfig.DiSEqC = i["DiSEqC"].InnerText; }
                }
                // Favoriten ermitteln
                nodes = root.SelectNodes("/Servicelist/Favoritelist/FavoriteGroup");
                foreach (XmlNode i in nodes)
                {
                    KFavoriteGroup lFav = new KFavoriteGroup();
                    ChannelFile.AddFavorite(lFav);
                    if (i["Name"] != null) { lFav.Name = i["Name"].InnerText; }
                    if (i["ServiceType"] != null)
                    {
                        if (i["ServiceType"].InnerText == "TV")
                        {
                            lFav.KServiceType = ServiceType.TV;
                        }
                        else
                        {
                            lFav.KServiceType = ServiceType.Radio;
                        }
                    }
                    //Kanalnummern suchen
                    XmlNodeList lChannels = i.SelectNodes("./ChannelNo");
                    foreach (XmlNode lChannel in lChannels)
                    {
                        lFav.AddChannel(Convert.ToInt32(lChannel.InnerText)); 
                    }
                    //Item der Listview hinzufügen
                    if (lFav.KServiceType == ServiceType.TV)
                    {
                        ListViewItem lItem = new ListViewItem(lFav.Data());
                        lItem.Tag = lFav; 
                        tvFavoritesListView.Items.Add(lItem);
                    }
                    else
                    {
                        ListViewItem lItem = new ListViewItem(lFav.Data());
                        lItem.Tag = lFav;
                        radioFavoritesListView.Items.Add(lItem);
                    }
                }

            }
            catch (Exception exc)
            {
                MessageBox.Show(String.Format("{0} Exception caught.", exc));
            }
        
        }

        //Datei speichern
        private void SaveToFile(string FileName)
        {
            //XML Dokument erstellen
            //XmlDocument doc = new XmlDocument();
            XmlDocument doc = ChannelFile.ToXML();
            //leider ist Kathrein nicht ganz Standard Konform, daher encoding noch anpassen
            string lxml = doc.ToString();
            lxml = lxml.Replace("&apos;", "'");
            System.IO.File.WriteAllText(FileName, lxml);
            //doc.Save(FileName);
        }

        private void dateiLadenClick(object sender, EventArgs e)
        {
            openFileDialog.InitialDirectory = LoadPath();
            openFileDialog.Title = "Kanalliste laden";
            openFileDialog.Filter = "XML Datei (*.xml)|*.xml|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                LoadFromFile(openFileDialog.FileName);
                SafePath(openFileDialog.FileName);
                dateiSpeichernMenuItem.Enabled = true;
            }
        }

        private void dateiSpeichernClick(object sender, EventArgs e)
        {
            saveFileDialog.InitialDirectory = LoadPath();
            saveFileDialog.Title = "Kanalliste speichern";
            saveFileDialog.Filter = "XML Datei (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                SaveToFile(saveFileDialog.FileName);
                SafePath(saveFileDialog.FileName);
            }
        }

        //mittels FTP Daten auf den Receiver speichern
        private void datenVCRSpeichernClick(object sender, EventArgs e)
        {
            FTPConnection ftpCon = new FTPConnection();
            string localFileName = Path.GetTempFileName();
            //auf eine lokale Datei zwischenspeichern
            SaveToFile(localFileName);
            ftpCon.Open(ftpUri, ftpUser, passwd, FTPMode.Passive);
            ftpCon.SetCurrentDirectory(ftpfilePath);
            ftpCon.SendFile(localFileName, ftpfileName, FTPFileTransferType.Binary);
            ftpCon.Close();
        }

        //Details zu den TV Favoriten anzeigen
        private void tvFavoritesListView_Click(object sender, EventArgs e)
        {
            if (tvFavoritesListView.SelectedItems.Count <= 0)
            {
                return;
            }
            tvDetailListView.Items.Clear();
            KFavoriteGroup lGroup = (KFavoriteGroup)tvFavoritesListView.SelectedItems[0].Tag;
            foreach (Int32 lChannel in lGroup.Channels){
                // Sender nach SenderNr suchen
                KService lService = ChannelFile.KService.Find(delegate(KService fs){return (fs.ChannelNo == lChannel) && (fs.KServiceType == ServiceType.TV);});
                string[] lData = {lChannel.ToString(), (lService != null) ? lService.Name : ""};
                tvDetailListView.Items.Add(new ListViewItem(lData));
            }
        }

        //Details zu den Radio Favoriten anzeigen
        private void radioFavoritesListView_Click(object sender, EventArgs e)
        {
            if (radioFavoritesListView.SelectedItems.Count <= 0)
            {
                return;
            }
            radioDetailListView.Items.Clear();
            KFavoriteGroup lGroup = (KFavoriteGroup)radioFavoritesListView.SelectedItems[0].Tag;
            foreach (Int32 lChannel in lGroup.Channels)
            {
                // Sender nach SenderNr suchen
                KService lService = ChannelFile.KService.Find(delegate(KService fs){return (fs.ChannelNo == lChannel) && (fs.KServiceType == ServiceType.Radio);});
                string[] lData = { lChannel.ToString(), (lService != null) ? lService.Name : "" };
                radioDetailListView.Items.Add(new ListViewItem(lData));
            }

        }

        //Löschen von TV Favoriten
        private void löschenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tvDetailListView.SelectedItems.Count <= 0)
            {
                return;
            }
            KFavoriteGroup lGroup = (KFavoriteGroup)tvFavoritesListView.SelectedItems[0].Tag;
            Int32 lEntry = Convert.ToInt32(tvDetailListView.SelectedItems[0].Text);
            lGroup.Channels.Remove(lEntry);
            //Liste neu aufbauen
            tvFavoritesListView_Click(sender, e);
        }

        //Löschen von Radio Favoriten
        private void löschenToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (radioDetailListView.SelectedItems.Count <= 0)
            {
                return;
            }
            KFavoriteGroup lGroup = (KFavoriteGroup)radioFavoritesListView.SelectedItems[0].Tag;
            Int32 lEntry = Convert.ToInt32(radioDetailListView.SelectedItems[0].Text);
            lGroup.Channels.Remove(lEntry);
            //Liste neu aufbauen
            radioFavoritesListView_Click(sender, e);
        }

        //umbenennen einer TV Favoriten Gruppe
        private void umbenennenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tvFavoritesListView.SelectedItems.Count <= 0)
            {
                return;
            }
            tvFavoritesListView.LabelEdit = true;
            tvFavoritesListView.SelectedItems[0].BeginEdit();
        }

        //nach dem Bearbeiten eines Textes diesen auf die Favoritengruppe schreiben
        private void tvFavoritesListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            if (tvFavoritesListView.SelectedItems.Count <= 0)
            {
                return;
            }
            KFavoriteGroup lGroup = (KFavoriteGroup)tvFavoritesListView.SelectedItems[0].Tag;
            lGroup.Name = tvFavoritesListView.SelectedItems[0].Text;
            tvFavoritesListView.LabelEdit = false;
        }

        //umbenennen einer Radio Favoriten Gruppe
        private void umbenennenToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (radioFavoritesListView.SelectedItems.Count <= 0)
            {
                return;
            }
            radioFavoritesListView.LabelEdit = true;
            radioFavoritesListView.SelectedItems[0].BeginEdit();
        }

        //nach dem Bearbeiten eines Textes diesen auf die Favoritengruppe schreiben
        private void radioFavoritesListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            if (radioFavoritesListView.SelectedItems.Count <= 0)
            {
                return;
            }
            KFavoriteGroup lGroup = (KFavoriteGroup)radioFavoritesListView.SelectedItems[0].Tag;
            lGroup.Name = radioFavoritesListView.SelectedItems[0].Text;
            radioFavoritesListView.LabelEdit = false;
        }

        //Drag & Drop Aktionen für TV Favoriten
        private void tvDetailListView_DragEnter(object sender, DragEventArgs e)
        {
            int len = e.Data.GetFormats().Length - 1;
            int i;
            for (i = 0; i <= len; i++)
            {
                if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
                {
                    //The data from the drag source is moved to the target. 
                    e.Effect = DragDropEffects.Copy;
                }
            }
        }

        #region Drag und Drop
        private void tvDetailListView_DragDrop(object sender, DragEventArgs e)
        {
            //Return if the items are not selected in the ListView control.
            if(tvListView.SelectedItems.Count==0)
            {
                return;
            }
            string lChannel = tvListView.SelectedItems[0].Text;
            //listView1.Items.Remove(dragItem);
            KFavoriteGroup lGroup = (KFavoriteGroup)tvFavoritesListView.SelectedItems[0].Tag;
            lGroup.AddChannel(Convert.ToInt32(lChannel));
            //Liste neu aufbauen
            tvFavoritesListView_Click(sender, e);
        }

        private void tvListView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            tvListView.DoDragDrop(tvListView.SelectedItems, DragDropEffects.Copy);
        }

        //Drag & Drop Aktionen für Radio Favoriten
        private void radioDetailListView_DragEnter(object sender, DragEventArgs e)
        {
            int len = e.Data.GetFormats().Length - 1;
            int i;
            for (i = 0; i <= len; i++)
            {
                if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
                {
                    //The data from the drag source is moved to the target. 
                    e.Effect = DragDropEffects.Copy;
                }
            }

        }

        private void radioDetailListView_DragDrop(object sender, DragEventArgs e)
        {
            //Return if the items are not selected in the ListView control.
            if (radioListView.SelectedItems.Count == 0)
            {
                return;
            }
            string lChannel = radioListView.SelectedItems[0].Text;
            KFavoriteGroup lGroup = (KFavoriteGroup)radioFavoritesListView.SelectedItems[0].Tag;
            lGroup.AddChannel(Convert.ToInt32(lChannel));
            //Liste neu aufbauen
            radioFavoritesListView_Click(sender, e);
        }

        private void radioListView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            radioListView.DoDragDrop(radioListView.SelectedItems, DragDropEffects.Copy);
        }

        #endregion

        private void kanalSperrenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)tvListView.SelectedItems[0].Tag;
            lService.Locked = true;
            tvListView.SelectedItems[0].SubItems[10].Text = lService.Locked.ToString(); 
        }

        private void kanalEntsperrenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)tvListView.SelectedItems[0].Tag;
            lService.Locked = true;
            tvListView.SelectedItems[0].SubItems[10].Text = lService.Locked.ToString(); 
        }

        private void kanalÜberspringenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)tvListView.SelectedItems[0].Tag;
            lService.Skipped = true;
            tvListView.SelectedItems[0].SubItems[9].Text = lService.Skipped.ToString(); 
        }

        private void kanalWiederEinbindenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)tvListView.SelectedItems[0].Tag;
            lService.Skipped = false;
            tvListView.SelectedItems[0].SubItems[9].Text = lService.Skipped.ToString(); 
        }

        private void kanalBearbeitenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditChannel lFrm = new EditChannel();
            lFrm.SetData((KService)tvListView.SelectedItems[0].Tag);
            if (lFrm.ShowDialog() == DialogResult.OK)
            {
                this.tvListView.BeginUpdate();
                //bearbeiteten Eintrag refreshen
                Int32 lItemIndex = tvListView.SelectedItems[0].Index;
                ListViewItem lItem = new ListViewItem(((KService)tvListView.SelectedItems[0].Tag).Data());
                lItem.Tag = (KService)tvListView.SelectedItems[0].Tag;
                //altes Element entfernen
                this.tvListView.SelectedItems[0].Remove();
                //neues Element einfügen
                this.tvListView.Items.Insert(lItemIndex, lItem);
                //Focus neu setzen
                this.tvListView.Items[lItemIndex].Focused = true;
                this.tvListView.Items[lItemIndex].Selected = true;
                this.tvListView.EndUpdate();
            }
         }

        private void senderSperrenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)radioListView.SelectedItems[0].Tag;
            lService.Locked = true;
            radioListView.SelectedItems[0].SubItems[10].Text = lService.Locked.ToString(); 
        }

        private void senderEntsperrenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)radioListView.SelectedItems[0].Tag;
            lService.Locked = true;
            radioListView.SelectedItems[0].SubItems[10].Text = lService.Locked.ToString(); 
        }

        private void senderÜberspringenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)radioListView.SelectedItems[0].Tag;
            lService.Skipped = true;
            radioListView.SelectedItems[0].SubItems[9].Text = lService.Skipped.ToString(); 
        }

        private void senderWiederEinbindenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KService lService = (KService)radioListView.SelectedItems[0].Tag;
            lService.Skipped = false;
            radioListView.SelectedItems[0].SubItems[9].Text = lService.Skipped.ToString(); 
        }

        private void senderBearbeitenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditChannel lFrm = new EditChannel();
            lFrm.SetData((KService)radioListView.SelectedItems[0].Tag);
            if (lFrm.ShowDialog() == DialogResult.OK)
            {
                this.radioListView.BeginUpdate();
                //bearbeiteten Eintrag refreshen
                Int32 lItemIndex = radioListView.SelectedItems[0].Index;
                ListViewItem lItem = new ListViewItem(((KService)radioListView.SelectedItems[0].Tag).Data());
                lItem.Tag = (KService)radioListView.SelectedItems[0].Tag;
                //altes Element entfernen
                this.radioListView.SelectedItems[0].Remove();
                //neues Element einfügen
                this.radioListView.Items.Insert(lItemIndex, lItem);
                //Focus neu setzen
                this.radioListView.Items[lItemIndex].Focused = true;
                this.radioListView.Items[lItemIndex].Selected = true;
                this.radioListView.EndUpdate();
            }
        }

        private void tvkanalLoeschen_Click(object sender, EventArgs e)
        {
            this.tvListView.BeginUpdate();
            //bearbeiteten Eintrag refreshen
            Int32 lItemIndex = tvListView.SelectedItems[0].Index;
            ListViewItem lItem = new ListViewItem(((KService)tvListView.SelectedItems[0].Tag).Data());
            KService lService = (KService)tvListView.SelectedItems[0].Tag;
            //Element entfernen
            this.tvListView.SelectedItems[0].Remove();
            tvList.Remove(lService);
            foreach (KService iService in tvList)
            {
                //alle verbelibenden Kanäle umnummerieren
                if (iService.ChannelNo > lService.ChannelNo) { iService.ChannelNo -= 1; }
            }
            for (Int32 i = lItemIndex; i < tvListView.Items.Count; i++) 
            {
                tvListView.Items[i].SubItems[0].Text = Convert.ToString(Convert.ToInt32(tvListView.Items[i].SubItems[0].Text) - 1);
            }
            //Focus neu setzen
            this.tvListView.Items[lItemIndex].Focused = true;
            this.tvListView.Items[lItemIndex].Selected = true;
            this.tvListView.EndUpdate();
        }

        private void radioSenderLoeschen_Click(object sender, EventArgs e)
        {
            this.radioListView.BeginUpdate();
            //bearbeiteten Eintrag refreshen
            Int32 lItemIndex = radioListView.SelectedItems[0].Index;
            ListViewItem lItem = new ListViewItem(((KService)radioListView.SelectedItems[0].Tag).Data());
            KService lService = (KService)radioListView.SelectedItems[0].Tag;
            //Element entfernen
            this.radioListView.SelectedItems[0].Remove();
            radioList.Remove(lService);
            foreach (KService iService in radioList)
            {
                //alle verbelibenden Kanäle umnummerieren
                if (iService.ChannelNo > lService.ChannelNo) { iService.ChannelNo -= 1; }
            }
            for (Int32 i = lItemIndex; i < radioListView.Items.Count; i++)
            {
                radioListView.Items[i].SubItems[0].Text = Convert.ToString(Convert.ToInt32(radioListView.Items[i].SubItems[0].Text) - 1);
            }
            //Focus neu setzen
            this.radioListView.Items[lItemIndex].Focused = true;
            this.radioListView.Items[lItemIndex].Selected = true;
            this.radioListView.EndUpdate();
        }

    }
}
